package familytree.yu.com.sso.config.jwt;

import familytree.yu.com.sso.model.LoginModel;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yuyunbo
 * @Date: 2021/2/22 21:38
 * @Description:
 */
public class Oauth2JwtTokenEnhancer  implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String, Object> info = new HashMap<>();
        //扩展JWT内容
        LoginModel loginModel = (LoginModel) authentication.getPrincipal();
        info.put("memberCode", loginModel.getMemberEntity().getMemberCode());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
        return accessToken;
    }
}
